package com.suse.forum.common;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;

@AllArgsConstructor
public class AppResult<T> {

    //状态码
    @Getter
    @Setter
    @JsonInclude(JsonInclude.Include.ALWAYS) // 不论任何情况都参与JSON序列化
    private int code;
    //描述信息
    @Getter
    @Setter
    @JsonInclude(JsonInclude.Include.ALWAYS)
    private String message;
    //具体的数据
    @Getter
    @Setter
    @JsonInclude(JsonInclude.Include.ALWAYS)
    private T data;

    /**
     * 构造方法
     * @param code
     * @param message
     */
    public AppResult(int code, String message) {
        this(code,message,null);
    }

    /**
     * 成功
     * @return
     */
    public static AppResult success() {
        return new AppResult(ResultCode.SUCCESS.getCode(),ResultCode.SUCCESS.getMessage());
    }

    public static AppResult success(String message) {
        return new AppResult((ResultCode.SUCCESS.getCode()),message);
    }

    public static <T>AppResult<T> success(T data)  {
        return new AppResult<>(ResultCode.SUCCESS.getCode(),ResultCode.SUCCESS.getMessage(),data);
    }

    public static <T> AppResult<T> success(String message,T data) {
        return new AppResult<>(ResultCode.SUCCESS.getCode(), message,data);
    }

    /**
     * 失败
     * @return
     */
    public static AppResult failed() {
        return new AppResult(ResultCode.FAILED.getCode(),ResultCode.FAILED.getMessage());
    }

    public static AppResult failed(String message) {
        return new AppResult(ResultCode.FAILED.getCode(),message);
    }

    public static AppResult failed(ResultCode resultCode) {
        return new AppResult(resultCode.getCode(),resultCode.getMessage());
    }
}
